<template>
  <el-row>
    <el-col :offset="8" :span="6" style="text-align: center">
      <h1>区块链教学示例系统</h1>
      <el-form label-width="80px" >
        <el-form-item label="用户名:">
          <el-input v-model="username"></el-input>
        </el-form-item>
        <el-form-item label="密码:">
          <el-input v-model="password" show-password></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="clearText">清空</el-button>
        </el-form-item>
      </el-form>

    </el-col>
  </el-row>


</template>

<script>
import config from '@/config'
export default {
  name: "Login",
  data() {
    return {
      username:'',
      password: ''
    }
  },
  methods: {
    clearText() {
      this.username = '';
      this.password = '';
    },
    login() {
      let that = this;
      console.log(config.base_url)
      let data = {
        'username': this.username,
        'password': this.password
      }
      this.axios.post(`/user/login`, data).then((response) => {
        if (response.data.code == 200) {
          console.log(response.data.data)
          let token = response.data.data.token;
          that.$cookies.set('token', token)
          that.$router.push("/")
        }else {
          alert(response.data.data)
        }

      }).catch((response) => {
        console.log(response)
        alert(`登录失败: ${response}`)
      })
    }
  }
}
</script>

<style scoped>

</style>